﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.JsonUtility
// Assembly: UnityEngine.JSONSerializeModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: A1060F3B-350A-4B1B-A0F2-6FBE9288E525
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.JSONSerializeModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.JSONSerializeModule.xml

using System;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;

#nullable disable
namespace UnityEngine
{
  /// <summary>
  ///   <para>Utility functions for working with JSON data.</para>
  /// </summary>
  public static class JsonUtility
  {
    private static  string ToJsonInternal(object obj, bool prettyPrint)
    {
      return JsonConvert.SerializeObject(obj, formatting: (prettyPrint ? Formatting.Indented: Formatting.None));
    }

    private static object FromJsonInternal(string json, object objectToOverwrite, System.Type type)
    {
      JsonConvert.PopulateObject(json,objectToOverwrite);
      return objectToOverwrite;
    }

    /// <summary>
    ///   <para>Generate a JSON representation of the public fields of an object.</para>
    /// </summary>
    /// <param name="obj">The object to convert to JSON form.</param>
    /// <param name="prettyPrint">If true, format the output for readability. If false, format the output for minimum size. Default is false.</param>
    /// <returns>
    ///   <para>The object's data in JSON format.</para>
    /// </returns>
    public static string ToJson(object obj) => JsonUtility.ToJson(obj, false);

    /// <summary>
    ///   <para>Generate a JSON representation of the public fields of an object.</para>
    /// </summary>
    /// <param name="obj">The object to convert to JSON form.</param>
    /// <param name="prettyPrint">If true, format the output for readability. If false, format the output for minimum size. Default is false.</param>
    /// <returns>
    ///   <para>The object's data in JSON format.</para>
    /// </returns>
    public static string ToJson(object obj, bool prettyPrint)
    {
      if (obj == null)
        return "";
      return (object) (obj as Object) == null || obj is MonoBehaviour || obj is ScriptableObject ? JsonUtility.ToJsonInternal(obj, prettyPrint) : throw new ArgumentException("JsonUtility.ToJson does not support engine types.");
    }

    public static T FromJson<T>(string json) => (T) JsonUtility.FromJson(json, typeof (T));

    /// <summary>
    ///   <para>Create an object from its JSON representation.</para>
    /// </summary>
    /// <param name="json">The JSON representation of the object.</param>
    /// <param name="type">The type of object represented by the Json.</param>
    /// <returns>
    ///   <para>An instance of the object.</para>
    /// </returns>
    public static object FromJson(string json, System.Type type)
    {
      if (string.IsNullOrEmpty(json))
        return (object) null;
      if (type == null)
        throw new ArgumentNullException(nameof (type));
      return !type.IsAbstract && !type.IsSubclassOf(typeof (Object)) ? JsonUtility.FromJsonInternal(json, (object) null, type) : throw new ArgumentException("Cannot deserialize JSON to new instances of type '" + type.Name + ".'");
    }

    /// <summary>
    ///   <para>Overwrite data in an object by reading from its JSON representation.</para>
    /// </summary>
    /// <param name="json">The JSON representation of the object.</param>
    /// <param name="objectToOverwrite">The object that should be overwritten.</param>
    public static void FromJsonOverwrite(string json, object objectToOverwrite)
    {
      if (string.IsNullOrEmpty(json))
        return;
      if (objectToOverwrite == null)
        throw new ArgumentNullException(nameof (objectToOverwrite));
      if ((object) (objectToOverwrite as Object) != null && !(objectToOverwrite is MonoBehaviour) && !(objectToOverwrite is ScriptableObject))
        throw new ArgumentException("Engine types cannot be overwritten from JSON outside of the Editor.");
      JsonUtility.FromJsonInternal(json, objectToOverwrite, objectToOverwrite.GetType());
    }
  }
}
